package com.dong.aoptools.view;

import com.dong.aoptools.constant.PropertiesConst;
import com.dong.aoptools.premain.handler.AOPHandler;
import com.dong.aoptools.premain.handler.CommonHandler;
import com.dong.aoptools.util.FontUtils;
import com.dong.aoptools.util.PathUtils;
import com.dong.aoptools.util.PropertiesUtils;
import com.dong.aoptools.view.handlerview.AOPHandlerView;
import com.dong.aoptools.view.handlerview.CommonHandlerView;
import com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel;
import javafx.scene.layout.HBox;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.nio.file.Path;
import java.util.*;

/**
 * @description: 首页
 * @author: 陈瑞东
 * @create: 2021-11-17 10:42
 */
public class IndexFrame extends JFrame {

    public static final int INDEX_WIDTH = 400;
    public static final int INDEX_HEIGHT = 300;

    public CommonHandlerView handlerView;

    public void init() {
        setTitle("配置页");
        // 设置窗体大小
//        setSize(new Dimension(INDEX_WIDTH, INDEX_HEIGHT));
        // 设置窗体关闭事件
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // 设置窗体居中
        setLocationRelativeTo(null);

        // 上部分配置
        String type = PropertiesUtils.getConfigProperty(PropertiesConst.CONFIG_FIELD_HANDLER_ACTIVE, PropertiesConst.TYPE_AOP);

        JComboBox<String> typeChooser = new JComboBox<String>();
        for (String item : PropertiesConst.HANDLER_VIEW_KEY_VALUE.keySet()) {
            String desc = PropertiesConst.HANDLER_DESC_KEY_VALUE.getOrDefault(item, "未知");
            typeChooser.addItem(desc);
            if (item.equals(type)) {
                // 选项切换至选中值
                typeChooser.setSelectedItem(desc);
                // 切换处理页面
                toggleHandlerView(desc);
            }
        }

        Box topVBox = Box.createVerticalBox();
        Box topInstructBox = Box.createHorizontalBox();
        Box topChooseBox = Box.createHorizontalBox();

        String instruct = "-javaagent:" + PathUtils.getJarPath();

        topInstructBox.add(new JLabel("代理指令："));
        topInstructBox.add(new JTextField(instruct));

        JRadioButton choose = new JRadioButton("选中", true);

        // 添加选项监听事件
        typeChooser.addActionListener(e -> {
            String activeDesc = typeChooser.getSelectedItem().toString();
            // 切换处理页面
            String item = toggleHandlerView(activeDesc);
            String active = PropertiesUtils.getConfigProperty(PropertiesConst.CONFIG_FIELD_HANDLER_ACTIVE, PropertiesConst.TYPE_AOP);
            choose.setSelected(active.equals(item));
        });

        // 添加单选框点击事件
        choose.addActionListener(e -> {
            // 如果点击必定是选中状态
            if (!choose.isSelected()) {
                choose.setSelected(true);
            }
            // 修改配置文件
            String item = getItemByDesc(typeChooser.getSelectedItem().toString());
            PropertiesUtils.setConfigProperty(PropertiesConst.CONFIG_FIELD_HANDLER_ACTIVE, item);
        });



        topChooseBox.add(new JLabel("功能选择："));
        topChooseBox.add(typeChooser);
        topChooseBox.add(Box.createHorizontalStrut(5));
        topChooseBox.add(choose);

        topVBox.add(topInstructBox);
        topVBox.add(new JPanel());
        topVBox.add(topChooseBox);

        add(topVBox, BorderLayout.NORTH);

//        add(new AOPHandlerView().init());
        // 窗体大小自适应
        pack();
        // 显示窗体
        setVisible(true);
    }

    /**
     * 切换处理页面
     * @param desc 选中的处理页的描述文字
     */
    private String toggleHandlerView(String desc) {
        // 遍历所有描述文字查询对应的标识符
        String item = getItemByDesc(desc);
        CommonHandlerView view = PropertiesConst.HANDLER_VIEW_KEY_VALUE.getOrDefault(item, new AOPHandlerView());
        if (handlerView != null) {
            remove(handlerView);
        }
        add(view.init(), BorderLayout.CENTER);
        handlerView = view;
        this.pack();
        return item;
    }

    /**
     * 根据选项描述获取页面标识
     * @param desc 选中的处理页的描述文字
     * @return 描述文字对应标识符
     */
    private String getItemByDesc(String desc) {
        // 遍历所有描述文字查询对应的标识符
        for (String item : PropertiesConst.HANDLER_VIEW_KEY_VALUE.keySet()) {
            String description = PropertiesConst.HANDLER_DESC_KEY_VALUE.getOrDefault(item, "未知");
            if (description.equals(desc)) {
                return item;
            }
        }
        return null;
    }

}
